<?php
require("include/config.inc.php");
require("include/mysql.inc.php");

$anno = @$_POST["anno"];

$db = new MySQL();

$db->connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME, DB_DEBUG);

$db->query("SELECT MIN(data_pub) AS primo, MAX(data_pub) AS ultimo FROM Annuncio_scaduto");

if($db->get_num_rows() > 0)
{
	$row = $db->fetch_array();
	
	$primo = date("Y", strtotime($row["primo"]));
	$ultimo = date("Y", strtotime($row["ultimo"]));
	
	for($i = $ultimo, $scelte = ""; $i >= $primo; $i--)
	{
		if($anno == $i)
			$scelte .= "<option value=\"$i\" selected>$i/" . ($i + 1) . "</option>\n";
		else
			$scelte .= "<option value=\"$i\">$i/" . ($i + 1) . "</option>\n";
	}
	
	$lista = "<form id=\"lista\" name=\"lista\" method=\"post\" action=\"scaduti.php\">
				<label>Selezionare l'Anno Accademico:
					<select name=\"anno\" id=\"anno\">
						<option value=\"null\">-- Seleziona --</option>
						$scelte
					</select>
				</label>
				<input type=\"submit\" name=\"go\" id=\"go\" value=\"Vai\">
			  </form>";
	
	if($anno != "")
	{
		$db->query("SELECT * FROM Annuncio_scaduto WHERE data_sca BETWEEN '" . $anno . "-10-01' AND '" . ($anno + 1) . "-09-30' ORDER BY data_sca DESC, codice DESC");
		
		if($db->get_num_rows() > 0)
		{
			$messaggi = "<b>ANNO ACCADEMICO $anno/" . ($anno + 1) . "</b><br><br>";
			
			while($row = $db->fetch_array())
			{
				$d_pub = date("d/m/Y", strtotime($row["data_pub"]));
				$d_sca = date("d/m/Y", strtotime($row["data_sca"]));
				
				$messaggi .= "<table style=\"table-layout: fixed; width:90%; margin-left: auto; margin-right: auto;\">
								<tr bgcolor=\"#FFDFBF\">
									<td><b>Pubblicato in data: <font color=\"#228888\" size=\"3\">$d_pub</font></b></td>
									<td align=\"right\"><b>Autore: </b>" . $row["autore"] . "</td>
									<td align=\"right\"><b>Scadenza: </b>$d_sca</td>
								</tr>
								<tr><td colspan=\"3\">" . str_replace("\n", "<br>", $row["corpo"]) . "</td></tr>
							  </table>";
			}
		}
		else
			$messaggi = "<span style=\"text-align: center; font-weight: bold; color: black;\">Non ci sono annunci scaduti.<br></span>";
	}
}
else
	$lista = "<span style=\"text-align: center; font-weight: bold; color: black;\">Non ci sono annunci scaduti.<br></span>";

$db->free_result();
$db->close();
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<style type="text/css" media="print">
TD {font-size:11px; font-family="arial,helvetica"}
P {text-indent:-10; text-align="justify"; margin-left:10;}
A:link {color: #44aaaa; text-decoration: underline;}
A:active {color: #000000; text-decoration: underline;}
A:visited {color: #227788; text-decoration: underline;}
A:hover {color: #000000; text-decoration: none; }
BODY {font-size:12px; font-family="arial,helvetica";}
FORM {margin-top:5}
</style>
<style type="text/css" media="screen">
TD {font-size:13px}
P {text-indent:-10; text-align="justify"; margin-left:10;}
A:link {color: #44aaaa; text-decoration: underline;}
A:active {color: #000000; text-decoration: underline;}
A:visited {color: #227788; text-decoration: underline;}
A:hover {color: #000000; text-decoration: none;}
BODY {background-image: url(images/captext.gif); font-size:14px}
FORM {margin-top:5}
</style>
</head>

<body>
<center><font color="#228888" size="4"><br><br><b>BACHECA: AVVISI SCADUTI</b><br><br></font></center>
<center>
<?php echo "$lista<br>$messaggi<br><br><a href=\"pubblicati.php\">Torna Indietro</a><br><br>"; ?>
</center>
<table bgcolor="#88CCCC" border="0" cellpadding="1" cellspacing="0" width="100%">
<tr>
	<td>Ultima modifica: <?php echo date("d/m/Y");?></td>
	<td align="right">Approvato da: Presidente CCdL</td>
</tr>
</table>
</body>
</html>